<form action="{:sysuri()}" method="post" data-auto="true" class="layui-form layui-card" data-table-id="EmployInfo">

    <div class="layui-card-body padding-left-40 layui-col-space10">

        <input  type="hidden" name="pid" value='{$vo.id|default=""}' required class="layui-input ">
        <input  type="hidden" name="app_id" value='{$vo.app_id|default=""}' required class="layui-input ">

         <input  type="hidden" name="level" value='{if $vo.pid == 0} 2 {else /} 3  {/if}' required class="layui-input ">
        {if $vo.pid != 0} <input  type="hidden" name="type" value='{$vo.type|default=""}' required class="layui-input "> {/if}


        <div class="layui-col-md12">
            <label class="layui-form-label">{:Lang("标题")}:</label>
            <div class="layui-input-block">

                <input name="title" value='' required class="layui-input ">
            </div>
        </div>
        {if $vo.pid == 0}
        <div class="layui-col-md12">
            <label class="layui-form-label">{:Lang("类型")}:</label>
            <div class="layui-input-block">

                {foreach $type_arr as $k=>$v}
                <input type="radio" name="type" value="{$k}" {if $k == $vo.type} checked {/if} title="{$v}">
                {/foreach}
            </div>
        </div>



        <div class="layui-col-md12 file_path" {if 1 == $vo.type || 0==$vo.type} style="display: none" {/if}>
            <label class="layui-form-label"></label>
            <div class="layui-input-block">
                <div style="display: flex;">
                    <a id="upload_button" class="layui-btn layui-bg-blue" >上传执行文件</a>
                    <input type="text" name="file_path" readonly class="layui-input "  value="">
                    <input type="file" style="display: none;" name="file_upload" id="file_upload" >
                </div>

            </div>
        </div>
        <div class="layui-col-md12 gold" {if 1 == $vo.type || 0==$vo.type } style="display: none" {/if}>

            <label class="layui-form-label">{:Lang("分数")}:</label>
            <div class="layui-input-block">

                <input name="gold" value='' placeholder="请输入分数" pattern="^[1-9][0-9]{0,1}$"  vali-name="分数"  required class="layui-input ">
            </div>
        </div>
        {/if}
        <div class="layui-col-md12">
            <label class="layui-form-label">{:Lang("排序")}:</label>
            <div class="layui-input-block">

                <input name="sort" value='{$vo.sort|default=1}' placeholder="请输入排序" pattern="^[1-9][0-9]{0,1}$"  vali-name="排序"  required class="layui-input ">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">{:Lang("实战详细描述")}:</label>
            <div class="layui-input-block" >
                <textarea name="content" placeholder="请输入实战详细描述" lay-verify="" class="layui-textarea" style="height: 350px;"></textarea>
                <p class="help-block"></p>
            </div>
        </div>
    </div>
    <div class="hr-line-dashed"></div>


    <div class="layui-form-item text-center">
        <button class="layui-btn" type='submit'>保存数据</button>
        <button class="layui-btn layui-btn-danger" type='button' data-confirm="确定要取消编辑吗？" data-close>取消编辑</button>
    </div>
</form>

<script>

    $(function () {
        // 异步加载富文本编辑器
        require(['ckeditor'], function () {
            setTimeout(function () {
                // 初始化富文本编辑器
                window.createEditor('[name=content]', {height: 350});
            },500)
        });
        // 指向所有 select 组件的选择事件
        layui.form.on('radio', function(data){

            if (data.value == 2) {
                $('.file_path').css('display', 'block')
                $('.gold').css('display', 'block')
               // $('input[name=file_path]').val('');
            } else {
                $('.file_path').css('display', 'none');
                $('.gold').css('display', 'none')
            }
        });
    });

    // 绑定点击事件处理器
    $("#upload_button").click(function() {

        $("#file_upload").trigger("click");
        // 使用示例
    });


    $("#file_upload").change(function () {
        const file = document.getElementById('file_upload').files[0];


        let formData = new FormData();
        formData.append('file', file);
        formData.append('fileName', file.name);

        fetch("{:sysuri('upload')}", {
            method: 'POST',
            body: formData
        }).then(response => {
            if (response.ok) {
                return response.json(); // 转换为JSON
            }
        }).then(data => {

            $('input[name=file_path]').val(data.data);

        })
    })
</script>